home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 010 / blit.arc / NEWLAYER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1985-05-23  |  4.5 KB  |  134 lines

  1. /*
  2.  * name:         newlayer
  3.  *
  4.  * description: create a new layer in rectangle r of bitmap *bp
  5.  *              and either fill the layer with optional halftone hb
  6.  *              or clear it. return a pointer to the new layer.
  7.  *
  8.  * synopsis:     struct layer   *newlayer (bp, r, hb)
  9.  *              struct bitmap   *bp;
  10.  *              struct rectangle    *r;
  11.  *              struct bitmap   *hb;
  12.  *
  13.  * globals:      obs          (r/w)
  14.  *              endobs        (r/w)
  15.  *              addpiece ()   (addpiece.c)
  16.  *              toplayer      (r/w)
  17.  *              bottomlayer   (r/w)
  18.  *              display       (r/w)
  19.  *
  20.  * calls:
  21.  *              rectxrect   (rectxrect.c)
  22.  *              addobs      (addobs.c)
  23.  *              bfree       (bfree.c)
  24.  *              free        (libc)
  25.  *              malloc      (libc)
  26.  *              layerop     (layerop.c)
  27.  *              balloc      (balloc.c)
  28.  *              upfront     (upfront.c)
  29.  *              bitblt      (bitblt.c)
  30.  *              rectf       (rectf.c)
  31.  *
  32.  * called by:    this is a top level function.
  33.  */
  34. #include "layers.h"
  35.  
  36. extern struct layer *toplayer;
  37. extern struct layer *bottomlayer;
  38. extern struct bitmap  *display;
  39.  
  40. struct obscured *obs;
  41. struct obscured *endobs;
  42.  
  43. struct layer  *newlayer (bp, r, hb)
  44. struct bitmap *bp;
  45. struct rectangle  *r;
  46. struct bitmap *hb;                     /* optional half tone to fill layer */
  47. {
  48.    struct layer    *lp;
  49.    struct layer    *newlp;
  50.    struct obscured *op;
  51.    struct obscured *nop;
  52.  
  53.    int      addpiece ();
  54.    struct bitmap   *balloc ();
  55.    char    *malloc ();
  56.  
  57.  /*
  58.   * first build, in obs, a list of all obscured rectangles that
  59.  * will be obscured by the new layer, doing subdivision with
  60.  * addobs()
  61.  */
  62.     obs = null;
  63.    endobs = null;
  64.    for (lp = toplayer; lp != null; lp = lp -> ly_back) {
  65.    /* lp = each layer from front to back */
  66.        for (op = lp -> ly_obs; op != null;) {
  67.         /* op = each obscured portion of lp */
  68.            nop = op;
  69.            op = op -> ob_next;
  70.            if (rectxrect (r, &(nop -> ob_rect)) &&
  71.                    addobs (nop, &(nop -> ob_rect), r, lp)) {
  72.             /*
  73.             * unlink nop from lp -> obs
  74.              */
  75.                if (nop -> ob_prev != null)
  76.                    nop -> ob_prev -> ob_next = nop -> ob_next;
  77.                else
  78.                    lp -> ly_obs = nop -> ob_next;
  79.                if (nop -> ob_next != null)
  80.                    nop -> ob_next -> ob_prev = nop -> ob_prev;
  81.                else
  82.                     lp -> ly_endobs = nop -> ob_prev;
  83.                (void) bfree (nop -> ob_bmap);
  84.                free ((char *) nop);
  85.            }
  86.        }
  87.    }
  88.  /*
  89.  * now add the rectangles not currently obscured, but that will
  90.  * be obscured by the new layer, by building the layer and
  91.  * calling layerop()
  92.  */
  93.  /*
  94.  * newlp = new layer
  95.  */
  96.    newlp = (struct layer   *) malloc ((unsigned) sizeof (struct layer));
  97.  /*
  98.  * bitmap part of newlp = bp
  99.  */
  100.    newlp -> ly_base = bp -> bm_base;         /* to be perfectly frank,  */
  101.    newlp -> ly_width = bp -> bm_width;       /* i don't understand what */
  102.                                              /* these are used for       */
  103.    newlp -> ly_rect.origin.x = r -> origin.x;
  104.    newlp -> ly_rect.origin.y = r -> origin.y;
  105.    newlp -> ly_rect.corner.x = r -> corner.x;
  106.    newlp -> ly_rect.corner.y = r -> corner.y;
  107.    newlp -> ly_obs = obs;                    /* currently obscured ... */
  108.    newlp -> ly_endobs = endobs;
  109.    for (lp = toplayer; lp != null; lp = lp -> ly_back)
  110.                                        /* lp = each layer from front to back */
  111.        (void) layerop (newlp, addpiece, &(lp -> ly_rect), lp, null, null, null);
  112.    newlp -> ly_obs = obs;                 /* ... and soon to be */
  113.    newlp -> ly_endobs = endobs;
  114.    for (op = obs; op != null; op = op -> ob_next)/* op = each element of obs */
  115.        op -> ob_bmap = balloc (&(op -> ob_rect));
  116.  /*
  117.  * link newlp into back of layer list
  118.  */
  119.    if (bottomlayer != null)
  120.        bottomlayer -> ly_back = newlp;
  121.    else                                /* first layer */
  122.        toplayer = newlp;
  123.    newlp -> ly_front = bottomlayer;
  124.    newlp -> ly_back = null;
  125.    bottomlayer = newlp;
  126.    (void) upfront (newlp);
  127.    if (hb != null)
  128.        (void) bitblt (null, &(newlp -> ly_rect), display, &(newlp -> ly_rect.origin), hb, s);
  129.    else
  130.        (void) rectf (display, &(newlp -> ly_rect), clr);
  131.  /* clear the screen rectangle */
  132.    return (newlp);
  133. }
  134.